<?php

namespace app\admin\service;

class KagupService
{


    function index($resutl, $project_id = 1)
    {


        $dh = $resutl['dh'];
        $content = $resutl['content'];
        //写文件


        $path = config("path");
        $filename = $dh . ".txt";
        $path = $path . "txt/" . $filename;
        var_dump($path);
        var_dump(222);
        $result = file_put_contents($path, $content);

        var_dump($result);
        var_dump(5555);
//        die();
        $resultPath = $this->uploadTxtToKay($path, $filename, $project_id);

        var_dump($dh);
        var_dump($project_id);
        var_dump($resultPath['path']);
//        die();

        if ($resultPath['code'] == 200) {


            $result = $this->submitCurl($resultPath['path'], $dh, $project_id);

            var_dump(7777);
            var_dump($result);

        }


        return $result;

    }

    // 上传文件
    function uploadTxtToKay($filename, $dh)
    {

// 初始化 cURL 会话
        $ch = curl_init();

// 设置请求 URL

        $kag = config('kag');

        $url = $kag . '/public/v1/reasoner/dialog/uploadFile';
        curl_setopt($ch, CURLOPT_URL, $url);

// 设置 HTTP 请求方法为 POST
        curl_setopt($ch, CURLOPT_POST, 1);

// 设置请求头
        $headers = [
            'Accept: */*',
            'Accept-Language: zh-CN,zh;q=0.9',
            'Connection: keep-alive',
            'Cookie: x-hng=lang=zh-CN; ctoken=bigfish_ctoken_19c1klbhlh',
            'Origin: ' . $kag,
            'Referer: ' . $kag,
            'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
            'X-Requested-With: XMLHttpRequest'
        ];

// 生成 boundary
        $boundary = '------------------------' . md5(time());

// 构建 multipart/form-data 数据
        $postData = '';

// 添加第一个文件

        $fileContent1 = file_get_contents($filename);
        $postData .= "--$boundary\r\n";
        $postData .= "Content-Disposition: form-data; name=\"file\"; filename=\"" . basename($dh) . "\"\r\n";
        $postData .= "Content-Type: text/plain\r\n\r\n";
        $postData .= $fileContent1 . "\r\n";


// 结束 boundary
        $postData .= "--$boundary--\r\n";

// 设置 Content-Type 头部
        $headers[] = "Content-Type: multipart/form-data; boundary=$boundary";

// 设置请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// 设置 POST 数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

// 设置返回响应而不是直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


// 执行 cURL 会话
        $response = curl_exec($ch);

// 检查是否有错误
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
// 关闭 cURL 会话
        curl_close($ch);

        $responseArr = json_decode($response, true);

        if ($responseArr['success'] == true) {
            return array('code' => 200, "path" => $responseArr['result']);
        } else {
            return array('code' => 500, "path" => "");
        }


    }

    function submitCurl($path, $dh, $project_id)
    {

// 初始化 cURL 会话

        $kag = config('kag');
// 设置请求 URL
        $ch = curl_init();

        $url = $kag . '/public/v1/builder/job/submit';

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 返回结果而不是直接输出
        curl_setopt($ch, CURLOPT_POST, 1); // 设置为POST请求
        $postData = '{"projectId":' . $project_id . ',"jobName":"' . $dh . '","type":"FILE_EXTRACT","fileUrl":"' . $path . '","extension":"{\"extractConfig\":{\"llmType\":\"DEFAULT\",\"llmPrompt\":\"\",\"autoSchema\":true,\"autoWrite\":true},\"splitConfig\":{\"splitLength\":\"2000\",\"semanticSplit\":false}}","createUser":"111111"}';


        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// 设置请求头
        $headers = array();
        $headers[] = 'Accept: application/json, text/plain, */*';
        $headers[] = 'Accept-Language: zh-CN,zh;q=0.9';
        $headers[] = 'Connection: keep-alive';
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Cookie: x-hng=lang=zh-CN; ctoken=bigfish_ctoken_19e887cg4a';
        $headers[] = 'Origin: http://192.168.2.108:8887';
        $headers[] = 'Referer: http://192.168.2.108:8887/';
        $headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


        $response = curl_exec($ch);

        var_dump($response);

        curl_close($ch);
// 关闭 cURL 会话
//        curl_close($ch);
        $arr = json_decode($response, true);


        return $arr;

    }

    function yy()
    {
        $ch = curl_init();
// 设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, "http://192.168.2.108:8887/public/v1/builder/job/submit?ctoken=bigfish_ctoken_19c20a7835");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 返回结果而不是直接输出
        curl_setopt($ch, CURLOPT_POST, 1); // 设置为POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, '{"projectId":3,"jobName":"216215","type":"FILE_EXTRACT","fileUrl":"/upload/0816cee7-9e96-491e-ae3b-1c5e7f75034d/216215.txt","extension":"{\"extractConfig\":{\"llmType\":\"DEFAULT\",\"llmPrompt\":\"\",\"autoSchema\":true,\"autoWrite\":true},\"splitConfig\":{\"splitLength\":\"2000\",\"semanticSplit\":false}}","createUser":"111111"}
');
// 设置请求头
        $headers = array();
        $headers[] = 'Accept: application/json, text/plain, */*';
        $headers[] = 'Accept-Language: zh-CN,zh;q=0.9';
        $headers[] = 'Connection: keep-alive';
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Cookie: x-hng=lang=zh-CN; ctoken=bigfish_ctoken_19e887cg4a';
        $headers[] = 'Origin: http://192.168.2.108:8887';
        $headers[] = 'Referer: http://192.168.2.108:8887/';
        $headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// 执行请求并获取响应
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }

        curl_close($ch);

// 输出响应数据
        echo $response;

    }


}